package com.ruoyi.web.controller.learn;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RequestMapping("/system/front")
@Controller
public class FrontController extends BaseController {

    @Autowired
    private ILearningContentService learningContentService;
    @Autowired
    private DictService dictService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IExamService examService;
    @Autowired
    private IAnswerRecordService recordService;
    @Autowired
    private ILikeTableService likeTableService;

    @RequestMapping("/learn")
    private String front(@RequestParam(required = false) String title, Model model){
        LearningContent learningContent = new LearningContent();
        learningContent.setTitle(title);
        List<LearningContent> contentList = learningContentService.selectLearningContentList(learningContent);
        model.addAttribute("user",getSysUser());
        model.addAttribute("contents",contentList);
        model.addAttribute("tags",dictService.getType("content_tags"));
        return "front";
    }

    @GetMapping("/detail/{contentId}")
    private String detail(@PathVariable("contentId")Long contentId, Model model){
        LearningContent content = learningContentService.selectLearningContentByContentId(contentId);
        model.addAttribute("user",getSysUser());
        model.addAttribute("content",content);
        Comment comment = new Comment();
        comment.setContentId(contentId);
        model.addAttribute("comments",commentService.selectCommentList(comment));
        AnswerRecord answerRecord = new AnswerRecord();
        if (Objects.isNull(getSysUser())){
            answerRecord.setUserId(999999L);
        }else {
            answerRecord.setUserId(getSysUser().getUserId());
        }
        answerRecord.setContentId(contentId);
        List<AnswerRecord> answerRecords = recordService.selectAnswerRecordList(answerRecord);
        model.addAttribute("answerRecords",answerRecords);
        LikeTable query = new LikeTable();
        query.setContentId(contentId);
        List<LikeTable> likeTables = likeTableService.selectLikeTableList(query);
        model.addAttribute("likeNum",likeTables.size());
        return "front_detail";
    }

    /**
     * 进行答题页
     * @param contentId
     * @param model
     * @return
     */
    @GetMapping("/answer/{contentId}")
    private String answer(@PathVariable("contentId")Long contentId,Model model) {
        if (Objects.isNull(getSysUser())) {
            return redirect("/login");
        }
        LearningContent content = learningContentService.selectLearningContentByContentId(contentId);
        Exam exam = new Exam();
        exam.setContentId(contentId);
        List<Exam> exams = examService.selectExamList(exam);
        model.addAttribute("user",getSysUser());
        model.addAttribute("content",content);
        model.addAttribute("exams",exams);
        return "front_answer";
    }


    /**
     * 查询答题记录页
     * @param contentId
     * @param model
     * @return
     */
    @GetMapping("/answerRecord/{contentId}")
    private String answerRecord(@PathVariable("contentId")Long contentId,Model model) {
        LearningContent content = learningContentService.selectLearningContentByContentId(contentId);
        model.addAttribute("user",getSysUser());
        model.addAttribute("content",content);
        AnswerRecord answerRecord = new AnswerRecord();
        if (Objects.isNull(getSysUser())){
            answerRecord.setUserId(999999L);
            answerRecord.setUserName("");
        }else {
            answerRecord.setUserId(getSysUser().getUserId());
            answerRecord.setUserName(getSysUser().getUserName());
        }
        answerRecord.setContentId(contentId);
        List<AnswerRecord> answerRecords = recordService.selectAnswerRecordList(answerRecord);
        model.addAttribute("answerRecords",answerRecords);
        return "front_answer_record";
    }

}
